#include "ffttestpage.h"
#include "application.h"
#include "ffttest.h"

FFTTestPage::FFTTestPage(QWidget *parent) : QWidget(parent)
{
    setWindowTitle(tr("FFTTestPage"));
    setAttribute(Qt::WA_DeleteOnClose, true);

    m_curve = new Curve();
    m_curve->setTitle("FFT Test");
    m_curve->setXAxisName(tr("cnt"));
    m_curve->addGraph(tr("raw data"), QColor(Qt::red));
    m_curve->addGraph(tr("fft data"), QColor(Qt::green));
    m_curve->addGraph(tr("ifft data"), QColor(Qt::blue));
    m_curve->addGraph(tr("ifft and conjugation data"), QColor(Qt::magenta));
    m_curve->addGraph(tr("ifft and rescale data"), QColor(Qt::yellow));
    m_curve->setGeometry(1000, 200, 800, 600);
    m_curve->show();

    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->addWidget(m_curve);
    setLayout(vbl);

    Application::getInstance()->subscribe(EV_CurveTest, this);

    // test
    FFTTest test;
//    test.test1();
//    test.test2();
    test.test3();
}

FFTTestPage::~FFTTestPage()
{
    if (m_curve)
        delete m_curve;
}

void FFTTestPage::customEvent(QEvent *event)
{
    quint16 id = event->type();
    switch (id)
    {
        case EV_CurveTest:
            if (m_curve != Q_NULLPTR)
            {
                CustomEvent *e = static_cast<CustomEvent*>(event);
                PlotXYParameter param = e->var.value<PlotXYParameter>();
                m_curve->onAddGraphPoint(param.num, param.x, param.y);
            }
        break;
        default:
        break;
    }
}

void FFTTestPage::closeEvent(QCloseEvent *event)
{
    Application::getInstance()->unsubscribe(EV_CurveTest, this);
    event->accept();
}
